package com.atguigu.spzx.user.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.atguigu.spzx.model.dto.user.UserLoginDto;
import com.atguigu.spzx.model.dto.user.UserRegisterDto;
import com.atguigu.spzx.model.entity.h5.UserCollect;
import com.atguigu.spzx.model.entity.order.OrderItem;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.UserCollectVo;
import com.atguigu.spzx.model.vo.user.UserInfoVo;
import com.atguigu.spzx.user.service.UserInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: UserInfoController
 * @Package: com.atguigu.spzx.user.controller
 * @Description 用户注册
 * @Author 孤妤
 * @Date: 2023/12/19 11:45
 */
@RestController
@RequestMapping("/api/user/userInfo")
@Tag(name = "用户管理")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result userRegist(@RequestBody UserRegisterDto userRegisterDto){
        userInfoService.userRegist(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result userLogin(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request){
        String token = userInfoService.userLogin(userLoginDto,request);
        return Result.build(token,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/getCurrentUserInfo")
    @Operation(summary = "获取用户信息")
    public Result findUser(){
        UserInfoVo userInfoVo = userInfoService.findUser();
        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "是否收藏商品")
    @GetMapping("/isCollect/{skuId}")
    public Result findIsCollect(@PathVariable Integer skuId){
        Boolean flat = userInfoService.findIsCollect(skuId);
        return Result.build(flat,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "收藏商品")
    @GetMapping("/auth/collect/{skuId}")
    public Result addCollect(@PathVariable Integer skuId){
        userInfoService.addCollect(skuId);
        return Result.build(true,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "取消收藏")
    @GetMapping("/auth/cancelCollect/{skuId}")
    public Result removeCancelCollect(@PathVariable Integer skuId){
        userInfoService.removeCancelCollect(skuId);
        return Result.build(true,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "我的收藏")
    @GetMapping("/auth/findUserCollectPage/{page}/{limit}")
    public Result findUserCollectPage(@PathVariable Integer page,
                                      @PathVariable Integer limit){
        PageInfo<UserCollectVo> userCollectPage = userInfoService.findUserCollectPage(page,limit);
        return Result.build(userCollectPage,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "浏览历史")
    @GetMapping("/auth/findUserBrowseHistoryPage/{page}/{limit}")
    public Result findUserBrowseHistoryPage(@PathVariable Integer page,
                                            @PathVariable Integer limit){
        PageInfo<UserCollectVo> userCollectVoPageInfo = userInfoService.findUserBrowseHistoryPage(page,limit);
        return Result.build(userCollectVoPageInfo,ResultCodeEnum.SUCCESS);
    }
}
